package com.soul.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class NumberUtil {

    public static final String STR_COMMA = ",";
    public static final String STR_EMPTY = "";

    public static void main(String[] args) {
        System.out.println(parseBigDecimal("100,001*.123", BigDecimal.ONE));
        System.out.println(parseBigDecimal("100,001  .123", new BigDecimal("2")));
        System.out.println(parseBigDecimal("  100,001.123  ", new BigDecimal("3")));
        System.out.println(parseBigDecimal(" --100,001.123 ", new BigDecimal("4")));
        System.out.println(parseBigDecimal(" +100,001.123 ", new BigDecimal("5")));
        System.out.println(parseBigDecimal("", new BigDecimal("6")));
        System.out.println(parseBigDecimal(" ", new BigDecimal("7")));
        System.out.println(parseBigDecimal(null, new BigDecimal("8")));
        System.out.println(parseBigDecimal(" +100,001.12.3 ", new BigDecimal("9")));
        System.out.println(parseBigDecimal(",", BigDecimal.TEN));
        System.out.println("-------- --------");

        System.out.println(parseBigDecimal2("100,001*.123", BigDecimal.ONE));
        System.out.println(parseBigDecimal2("100,001  .123", new BigDecimal("2")));
        System.out.println(parseBigDecimal2("  100,001.123  ", new BigDecimal("3")));
        System.out.println(parseBigDecimal2(" --100,001.123 ", new BigDecimal("4")));
        System.out.println(parseBigDecimal2(" +100,001.123 ", new BigDecimal("5")));
        System.out.println(parseBigDecimal2("", new BigDecimal("6")));
        System.out.println(parseBigDecimal2(" ", new BigDecimal("7")));
        System.out.println(parseBigDecimal2(null, new BigDecimal("8")));
        System.out.println(parseBigDecimal2(" +100,001.12.3 ", new BigDecimal("9")));
        System.out.println(parseBigDecimal2(".", new BigDecimal("10")));
        System.out.println(parseBigDecimal2("+", new BigDecimal("11")));
        System.out.println(parseBigDecimal2(",", new BigDecimal("12")));
        System.out.println("-------- --------");

        System.out.println(BigDecimal.ZERO);
        System.out.println("-------- --------");

    }

    /**
     * 将字符串转换为BigDecimal
     *
     * @param str          数字字符串
     * @param defaultValue 默认值
     * @return BigDecimal
     */
    public static BigDecimal parseBigDecimal(String str, BigDecimal defaultValue) {
        if (StringUtils.isBlank(str)) {
            return defaultValue;
        }
        if (str.contains("--")) {
            return defaultValue;
        }
        if (str.contains(",")) {
            str = str.replaceAll(",", "");
        }
        if (StringUtils.isBlank(str)) {
            return defaultValue;
        }
        try {
            return new BigDecimal(str.trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static BigDecimal parseBigDecimal2(String sourceStr, BigDecimal defaultValue) {
        //允许逗号分隔
        if (null != sourceStr && sourceStr.contains(STR_COMMA)) {
            sourceStr = sourceStr.replaceAll(STR_COMMA, STR_EMPTY);
        }
        //允许有空格
        if (null != sourceStr) {
            sourceStr = sourceStr.trim();
        }
        if (!isNumber(sourceStr)) {
            return defaultValue;
        }
        return new BigDecimal(sourceStr);
    }

    /**
     * ASCII
     *
     * @param str 字符串
     * @return 是否为数字
     */
    public static boolean isNumber(String str) {
        if (null == str || str.length() == 0) {
            return false;
        }

        StringBuilder stringBuilder = deleteChar(str);
        //有删除操作就需要重新判空
        if (stringBuilder.length() == 0) {
            return false;
        }

        for (int i = stringBuilder.length(); --i >= 0; ) {
            int chr = stringBuilder.charAt(i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }

    /**
     * 剔除首位的+ -号, 剔除第一次找到的 .
     *
     * @param str 字符串
     * @return 删除操作, 注意判断字符串长度
     */
    public static StringBuilder deleteChar(String str) {
        int flag;
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        if (sb.charAt(0) == '+' || sb.charAt(0) == '-') {
            sb.deleteCharAt(0);
        }
        //只剔除第一个 . (数字仅能含有一个.)
        if ((flag = sb.indexOf(".")) != -1) {
            sb.deleteCharAt(flag);
        }
        return sb;
    }

}
